<script setup>
import { ref, watch } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { userRegisterService, userLoginService } from '../../api/user/user'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores'
/***
 * 1.表单业务
 ***/
//是否为注册表单
const isRegister = ref(false)
//重置所有表单
const resetAllForm = () => {
  formLogin.value = {
    username: '',
    password: ''
  }
  formRegister.value = {
    username: '',
    password: '',
    repassword: ''
  }
}
//监听isRegister的变化
watch(
  () => isRegister.value,
  () => {
    resetAllForm()
  }
)
//1.1 登入表单
const formLogin = ref({
  username: '',
  password: ''
})
//1.1.1登入规则
const fromLoginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ]
}
//1.1.2登入按钮
const formLoginRef = ref()
const router = useRouter()
const userStore = useUserStore()
const onSubmitLogin = async () => {
  await formLoginRef.value.validate()
  const { data: res } = await userLoginService(formLogin.value)
  if (res.code === 0) {
    userStore.setToken(res.token)
    ElMessage.success(res.message)
    router.push('/')
  } else {
    ElMessage.error(res.message)
    resetAllForm()
  }
}
/**
 * 1.2 注册表单
 **/
const formRegister = ref({
  username: '',
  password: '',
  repassword: ''
})
//1.2.1表单校验
const formRegisterRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formRegister.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
const formRegisterRef = ref()
//注册
const onSubmitRegister = async () => {
  await formRegisterRef.value.validate()
  const { data: res } = await userRegisterService(formRegister.value)
  if (res.code === 0) {
    // 切换到登录
    isRegister.value = false
    ElMessage({ message: res.message, type: 'success' })
  } else {
    ElMessage.error(res.message)
  }
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"> </el-col>
    <el-col :span="6" :offset="3" class="form">
      <el-form
        :model="formRegister"
        ref="formRegisterRef"
        size="large"
        v-if="isRegister"
        :rules="formRegisterRules"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formRegister.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formRegister.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formRegister.repassword"
            :prefix-icon="Lock"
            placeholder="请再次输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" size="large" @click="onSubmitRegister"
            >注册</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="primary" :underline="false" @click="isRegister = false">返回 →</el-link>
        </el-form-item>
      </el-form>
      <!-- 登 入 -->
      <el-form :model="formLogin" ref="formLoginRef" size="large" :rules="fromLoginRules" v-else>
        <el-form-item>
          <h1>登入</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="formLogin.username" :prefix-icon="User" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formLogin.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>

        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" size="large" @click="onSubmitLogin"
            >登入</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link type="primary" :underline="false" @click="isRegister = true">注册 →</el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<style lang="scss" scoped>
.login-page {
  height: 100vh;
  .bg {
    background-image: url('@/assets/image/bigThing.png');
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    margin-top: 25vh;
    height: 50vh;
    justify-content: center;
    flex-direction: column;
  }
}

.flex {
  display: flex;
  width: 100%;
  justify-content: space-between;
}
</style>
